/**
 * 网络请求配置
 */
import axios from "axios";
import { getToken, removeToken } from '../utils/auth'

axios.defaults.timeout = 100000;
axios.defaults.baseURL = process.env.REACT_APP_URL;
// console.log(process,axios.defaults.baseURL);

/**
 * http request 拦截器
 */
axios.interceptors.request.use(
  (config) => {
    // console.log(config, config.url);
    const { url } = config;
    /* config.data = JSON.stringify(config.data);
    config.headers = {
      "Content-Type": "application/json",
    }; */
    /* console.log(url.startsWith('/user') &&
    !url.startsWith('/user/login') &&
    !url.startsWith('/user/registered')); */
    if (
      url.startsWith('/user') &&
      !url.startsWith('/user/login') &&
      !url.startsWith('/user/registered')
    ) {
      // 添加请求头
      config.headers.Authorization = getToken();
    } else {
      if (url.startsWith("/favorites") || url.startsWith("/modifyuserinfo")) {
        // 添加请求头
        config.headers.Authorization = getToken();
      }
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

/**
 * http response 拦截器
 */
axios.interceptors.response.use(
  (response) => {
    /* if (response.data.errCode === 2) {
      console.log("过期");
    } */
    const { status } = response.data;
    if (status === 400) {
      // 此时，说明 token 失效，直接移除 token 即可
      removeToken();
    }
    return response;
  },
  (error) => {
    console.log("请求出错：", error);
  }
);

/**
 * 封装get方法
 * @param url  请求url
 * @param params  请求参数
 * @returns {Promise}
 */
function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params,
    }).then((response) => {
      // landing(url, params, response.data);
      resolve(response.data);
    })
      .catch((error) => {
        reject(error);
      });
  });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

function post(url, data) {
  return new Promise((resolve, reject) => {
    axios.post(url, data).then(
      (response) => {
        //关闭进度条
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}

/**
 * 封装delete方法
 * @param url  请求url
 * @param params  请求参数
 * @returns {Promise}
 */
function deleteAxios(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.delete(url, {
      params: params,
    }).then((response) => {
      // landing(url, params, response.data);
      resolve(response.data);
    })
      .catch((error) => {
        reject(error);
      });
  });
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */
function patch(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.patch(url, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        // msag(err);
        reject(err);
      }
    );
  });
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

/* function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(url, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        msag(err);
        reject(err);
      }
    );
  });
} */

//统一接口处理，返回数据
export function api(fecth, url, param) {
  return new Promise((resolve, reject) => {
    switch (fecth) {
      case "get":
        // console.log("begin a get request,and url:", url);
        get(url, param)
          .then(function (response) {
            resolve(response);
          })
          .catch(function (error) {
            console.log("get request GET failed.", error);
            reject(error);
          });
        break;
      case "post":
        post(url, param)
          .then(function (response) {
            resolve(response);
          })
          .catch(function (error) {
            console.log("get request POST failed.", error);
            reject(error);
          });
        break;
      case "delete":
        // console.log("begin a get request,and url:", url);
        deleteAxios(url, param)
          .then(function (response) {
            resolve(response);
          })
          .catch(function (error) {
            console.log("get request DELETE failed.", error);
            reject(error);
          });
        break;
      case "patch":
        // console.log("begin a get request,and url:", url);
        patch(url, param)
          .then(function (response) {
            resolve(response);
          })
          .catch(function (error) {
            console.log("get request PATCH failed.", error);
            reject(error);
          });
        break;
      default:
        break;
    }
  });
}

//失败提示
/* function msag(err) {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        alert(err.response.data.error.details);
        break;
      case 401:
        alert("未授权，请登录");
        break;

      case 403:
        alert("拒绝访问");
        break;

      case 404:
        alert("请求地址出错");
        break;

      case 408:
        alert("请求超时");
        break;

      case 500:
        alert("服务器内部错误");
        break;

      case 501:
        alert("服务未实现");
        break;

      case 502:
        alert("网关错误");
        break;

      case 503:
        alert("服务不可用");
        break;

      case 504:
        alert("网关超时");
        break;

      case 505:
        alert("HTTP版本不受支持");
        break;
      default:
    }
  }
} */

/**
 * 查看返回的数据
 * @param url
 * @param params
 * @param data
 */
/* function landing(url, params, data) {
  if (data.code === -1) {
  }
} */

